package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannerMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Api(tags = "频道管理API",value = "频道管理api")
@Service
@Slf4j
public class ChannelServiceImpl extends ServiceImpl<ChannerMapper, AdChannel> implements ChannelService {



    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        //1.检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();//检查分页参数

        //2.封装查询参数
        //2.1 分页参数
        IPage pageReq = new Page(dto.getPage(),dto.getSize());
        //2.2 条件参数
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNoneBlank(dto.getName())){
           queryWrapper.like(AdChannel::getName,dto.getName());

        }
        //3. 执行查询
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //int i=1/0;//模拟异常出现
        //4.封装响应结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());//分页的结果
        return responseResult;

    }

/*插入频道数据*/
    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.检查参数不能为空
        if (channel==null || StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空！！");
        }
        //2.判断该频道是否存在数据库中
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在，请重新输入！！");
        }

        //3.保存频道
        this.save(channel);
        channel.setCreatedTime(new Date());
        //3.f回结果
        return ResponseResult.okResult("添加成功");
    }

    /*
    修改频道
    * */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数校验
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2 根据id查询频道信息
        AdChannel byId = this.getById(adChannel.getId());
        if (byId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //3. 如果频道名称不为空，且输入频道名称和原频道名称不一致
        if(StringUtils.isNotBlank(adChannel.getName())
                &&
                !adChannel.getName().equals(byId.getName())){
            //判断是否一致
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, adChannel.getName()));
            if(count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        updateById(adChannel);
        //4 返回结果
        return ResponseResult.okResult();

    }

    @Override
    @Transactional
    public ResponseResult delete(Integer id) {
//        int i = 1/0; //模拟异常
//        if(id == 1){
//           // throw new CustomException(AppHttpCodeEnum.PARAM_INVALID,"id不能为1");
//            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"id不能为1");
//        }
        //检查参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        //根据id查询对应频道数据
        AdChannel channel = this.getById(id);
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        检查频道状态，是否启用
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }
        //删除频道
        this.removeById(id);


        //返回
        return ResponseResult.okResult();
    }

}
